package com.ccp.dev.workflow.service;

import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.PropertyUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.workflow.dao.TaskReminderDao;
import com.ccp.dev.workflow.model.TaskReminder;
import com.ccp.dev.workflow.model.WarningSetting;
import net.sf.json.JSONArray;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * ClassDescribe:任务催办 Service类
 * @author :jiayueqiao
 * Date: 2019-10-21
 * Since:1
 */
@Service
public class TaskReminderService extends BaseService<TaskReminder> {

    @Resource
    private TaskReminderDao dao;

    /**
     * 根据流程定义Id和节点Id获取催办信息。
     * @param procDefId 流程定义id
     * @param nodeId 节点id
     * @return 催办设置列表
     */
    public List<TaskReminder> queryByProcDefAndNodeId(String procDefId, String nodeId){
        return dao.queryByProcDefAndNodeId(procDefId, nodeId);
    }

    /**
     * 获取系统属性中的任务预警json数据
     * @return 预警信息列表
     */
    public List<WarningSetting> getWaringSettingList(){
        String json = PropertyUtil.getByAlias("task.WarnLevel");
        if(StringUtil.isEmpty(json)){
            return Collections.emptyList();
        }
        return JSONObject.parseArray(json,WarningSetting.class);
    }

    /**
     * 获取预警颜色json数据
     * @return 预警颜色json数据
     */
    public String getWaringSetJson(){
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("priority", TaskEntity.DEFAULT_PRIORITY);
        object.put("color", "blue");
        array.add(object);
        List<WarningSetting> warningSettingList  = getWaringSettingList();
        if(BeanUtils.isEmpty(warningSettingList)){
            return array.toString();
        }
        for (WarningSetting setting:warningSettingList) {
            JSONObject o = new JSONObject();
            o.put("priority", setting.getLevel());
            o.put("color", setting.getColor());
            array.add(o);
        }
        return array.toString();
    }

    /**
     * 根据流程定义id获取催办设置列表
     * @param procDefId 流程定义id
     * @return 催办设置列表
     */
    public List<TaskReminder> queryByProcDefId(String procDefId) {
        return dao.queryByProcDefId(procDefId);
    }
}
